<template>
        <view class="uni-container">
			<el-dialog title="添加发行" :visible.sync="dialogFormVisible" width="80%">
			  <el-form :model="form">
				<el-form-item label="发行ID" :label-width="formLabelWidth">
				  <el-input v-model="form.name" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="发行名称" :label-width="formLabelWidth">
				  <el-input v-model="form.des" autocomplete="off"></el-input>
				</el-form-item>
			  </el-form>
			  <div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addEdition">确 定</el-button>
			  </div>
			</el-dialog>
        <view class="uni-header" v-if="$hasRole('admin')">
            <view class="uni-group">
				<el-button type="primary" @click="dialogFormVisible = true" round size="mini">添加发行</el-button>
            </view>
        </view>
            <uni-table :loading="loading" border  emptyText="暂无更多数据" @selection-change="selectionChange">
                <uni-tr>
					<uni-th width="100" align="center">发行ID</uni-th>
					<uni-th width="100" align="center">发行名称</uni-th>
					<uni-th width="100" align="center">今日收益</uni-th>
					<uni-th width="100" align="center">本周收益</uni-th>
                    <uni-th width="100" align="center">本月收益</uni-th>
					<uni-th width="100" align="center">上月收益</uni-th>
					<uni-th width="100" align="center">总收益</uni-th>
					<uni-th width="100" align="center">总版本</uni-th>
					<uni-th width="100" align="center">活跃版本</uni-th>
					<uni-th width="100" align="center">最后上线</uni-th>
					<uni-th width="100" align="center"  v-if="$hasRole('admin')">空档天数</uni-th>
                    <uni-th width="100" align="center"  v-if="$hasRole('admin')">设置</uni-th>
                    <!-- <uni-th width="204" align="center">设置</uni-th> -->
                </uni-tr>
                <uni-tr v-for="(item ,index) in tableData" :key="index">
					<uni-td>{{item.id}}</uni-td>
					<uni-td>{{item.name}}</uni-td>
					<uni-td>{{item.day}}</uni-td>
					<uni-td>{{item.week}}</uni-td>
					<uni-td>{{item.month}}</uni-td>
					<uni-td>{{item.b_month}}</uni-td>
					<uni-td>{{item.all}}</uni-td>
					<uni-td>{{item.count}}</uni-td>
					<uni-td>{{item.active}}</uni-td>
					<uni-td>{{item.last}}</uni-td>
					<uni-td  v-if="$hasRole('admin')">{{item.idle}}</uni-td>
					<uni-td  v-if="$hasRole('admin')">
                        <view class="uni-group">
							<button class="uni-button" size="mini" type="warn" @click="delEdition(item.id)">删除</button>
                        </view>
                    </uni-td>
                </uni-tr>
            </uni-table>
            <view class="uni-pagination-box">
                <uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total" @change="change" />
            </view>
        </view>
		<!-- #ifndef H5 -->
		<fix-window />
		<!-- #endif -->
    </view>
</template>

<script>
    export default {
        data() {
            return {
				dialogFormVisible : false,
				formLabelWidth : "70px",
				form: {
					name : "",
					des : ""
				},
				serverData : [],
                tableData: [],
                // 每页数据量
                pageSize: 100,
                // 当前页
                pageCurrent: 1,
                // 数据总量
                total: 0,
                loading: false
            }
        },
        onLoad() {
            this.selectedIndexs = []
            this.getEditions(1)
        },
        methods: {
            // 分页触发
            change(e) {
                this.getEditions(e.current)
            },
			//获取发行列表
			getEditions(pageCurrent){
				console.log("getData!!!!!!!!!!!!!!!!!!!!!!",pageCurrent)
                this.loading = true
                this.pageCurrent = pageCurrent
				this.request("/getPublishData",{},(res) => {
						console.log('data!!!!!!!', res.data);
						var list = []
						if(res.data){
							for(var i = 0;i < res.data.length;i++){
								list[i] = res.data[i]
								if(list[i].lastTime == -1)
									list[i].idle = "未上线"
								else
									list[i].idle = Math.ceil((Date.now() - list[i].lastTime) / 86400000) + "天"
							}
						}
						this.serverData = list
						this.total = list.length
						console.log("serverData!!!",this.serverData)
						this.tableData = this.serverData.slice((this.pageCurrent-1)*this.pageSize,this.pageCurrent*this.pageSize)
						console.log("tableData!!!",this.tableData)
						this.loading = false
				})
			},
			//修改发行
			changeEdition(index){
				console.log(this.tableData[index])
				this.form = this.tableData[index]
				this.dialogFormVisible = true
			},
			//添加发行
			addEdition(){
				this.dialogFormVisible = false
				var info = {
					name : this.form.name,
					des : this.form.des
				}
				this.request("/addPublish",info,(res) => {
						this.$message({
						  message: '添加成功',
						  type: 'success'
						});
						this.getEditions(1)
						console.log('data!!!!!!!', res.data);
				})
			},
			//删除发行
			delEdition(name){
				this.request("/delPublish",{name:name},(res) => {
						console.log('data!!!!!!!', res.data);
							this.$message({
							  message: '删除发行',
							  type: 'success'
							});
						this.getEditions(1)
				})
			},
            // request请求
            request(path,options,success) {
				this.$axios.post(path,options)//传参
				.then(function(res){
					  success(res)
				})
				.catch(function(err){
					  console.log("请求失败233");
				});
            }
        }
    }
</script>

<style>
	/* #ifndef H5 */
	page {
		padding-top: 85px;
	}
	/* #endif */
</style>
